use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, PartialEq, Eq, DeriveValueType, Serialize, Deserialize)]
pub struct Id(pub i32);

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "cms_article")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    /// 所属专题
    pub topic_id: i32,
    /// 文章标题
    #[sea_orm(column_type = "String(StringLen::N(64))", indexed)]
    pub title: String,
    /// 作者
    #[sea_orm(column_type = "String(StringLen::N(32))")]
    pub author: String,
    /// 转载来源网址
    pub fromurl: String,
    /// 封面图
    pub image: String,
    /// 关键词
    pub keywords: String,
    /// 简介
    pub description: String,
    /// 是否允许评论
    pub allow_comments: bool,
    /// 状态
    /// - 0 待发布
    /// - 1 审核中
    /// - 2 已发布
    /// - 3 被驳回
    /// - `>=64` 回收站
    #[sea_orm(indexed)]
    pub status: u8,
    /// 发布人id
    #[sea_orm(indexed)]
    pub user_id: i32,
    /// 阅读数量
    pub read_number: u32,
    /// 内容
    #[sea_orm(column_type = "custom(\"LONGTEXT\")")]
    pub content: String,
    pub is_markdown: bool,
    #[sea_orm(indexed)]
    pub publish_at: chrono::NaiveDateTime,
    pub created_at: chrono::NaiveDateTime,
    pub updated_at: chrono::NaiveDateTime,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl Related<super::cms_category::Entity> for Entity {
    fn to() -> RelationDef {
        super::cms_article_category::Relation::CmsCategory.def()
    }

    fn via() -> Option<RelationDef> {
        Some(
            super::cms_article_category::Relation::CmsArticle
                .def()
                .rev(),
        )
    }
}

impl ActiveModelBehavior for ActiveModel {}
